package cn.itcast.bos.base.service.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.base.dao.CourierDao;
import cn.itcast.bos.base.dao.FixedAreaDao;
import cn.itcast.bos.base.dao.TakeTimeDao;
import cn.itcast.bos.base.service.FixedAreaService;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.domain.base.TakeTime;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {
	
	@Autowired
	private FixedAreaDao fixedAreaDao;
	@Autowired
	private CourierDao courierdao;
	@Autowired
	private TakeTimeDao taketimedao;
	@Override
	public void AddFixedArea(FixedArea fixedArea) {
		// TODO Auto-generated method stub
		fixedAreaDao.save(fixedArea);
	}
	
	public  Page showFixedArea(Specification<FixedArea> spec,Pageable pageable){
		return fixedAreaDao.findAll(spec, pageable);
	}

	@Override
	public List<Courier> getgetCourier() {
		// TODO Auto-generated method stub
		Specification<Courier> spec=new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				// TODO Auto-generated method stub
//				Predicate notNull = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				Predicate empty = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return empty;
			}
		};
		return courierdao.findAll(spec);
	}

	@Override
	public List<TakeTime> getTaketime() {
		// TODO Auto-generated method stub
		return taketimedao.findAll();
	}

	@Override
	public void fixedArea_associationCourierToFixedArea(String id,
			Integer courierId, Integer takeTimeId) {
		// TODO Auto-generated method stub
		FixedArea fixedArea = fixedAreaDao.getOne(id);
		TakeTime takeTime = taketimedao.getOne(takeTimeId);
		Courier courier = courierdao.getOne(courierId);
		courier.setTakeTime(takeTime);
		fixedArea.getCouriers().add(courier);
	}

	@Override
	public FixedArea findById(String id) {
		// TODO Auto-generated method stub
		return fixedAreaDao.findOne(id);
	}

}
